using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.LookAndFeel;
using Padding = System.Windows.Forms.Padding;

namespace Demo
{
    public partial class SkinSelectionForm : Form
    {
        private ListBoxControl listBoxSkins;
        private SimpleButton btnOK;
        private SimpleButton btnCancel;
        private SimpleButton btnPreview;
        private LabelControl lblCurrentSkin;
        private LabelControl lblSkinType;
        private string originalSkin;
        
        public string SelectedSkin { get; private set; } = string.Empty;
        
        public SkinSelectionForm()
        {
            InitializeComponent();
            originalSkin = SkinManager.GetCurrentSkin();
            LoadSkins();
            SelectCurrentSkin();
        }
        
        private void InitializeComponent()
        {
            this.Text = "选择皮肤";
            this.Size = new Size(550, 600);
            this.StartPosition = FormStartPosition.CenterParent;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            
            var panel = new TableLayoutPanel();
            panel.Dock = DockStyle.Fill;
            panel.RowCount = 4;
            panel.ColumnCount = 1;
            panel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            panel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            panel.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
            panel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            panel.Padding = new Padding(15);
            
            // 当前皮肤标签
            lblCurrentSkin = new LabelControl();
            lblCurrentSkin.Text = $"当前皮肤: {SkinManager.GetCurrentSkin()}";
            lblCurrentSkin.AutoSizeMode = LabelAutoSizeMode.None;
            lblCurrentSkin.Margin = new Padding(0, 0, 0, 10);
            lblCurrentSkin.Appearance.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Bold);
            lblCurrentSkin.Appearance.ForeColor = Color.FromArgb(64, 64, 64);
            panel.Controls.Add(lblCurrentSkin, 0, 0);
            
            // 皮肤类型标签
            lblSkinType = new LabelControl();
            lblSkinType.Text = "提示：选择皮肤后可以点击\"预览\"按钮查看效果";
            lblSkinType.AutoSizeMode = LabelAutoSizeMode.None;
            lblSkinType.Margin = new Padding(0, 0, 0, 10);
            lblSkinType.Appearance.Font = new Font("Microsoft YaHei UI", 8.5F);
            lblSkinType.Appearance.ForeColor = Color.FromArgb(128, 128, 128);
            panel.Controls.Add(lblSkinType, 0, 1);
            
            // 皮肤列表
            listBoxSkins = new ListBoxControl();
            listBoxSkins.Dock = DockStyle.Fill;
            listBoxSkins.DisplayMember = "Text";
            listBoxSkins.ValueMember = "Value";
            listBoxSkins.SelectedValueChanged += ListBoxSkins_SelectedValueChanged;
            listBoxSkins.Appearance.Font = new Font("Microsoft YaHei UI", 9F);
            panel.Controls.Add(listBoxSkins, 0, 2);
            
            // 按钮面板
            var btnPanel = new FlowLayoutPanel();
            btnPanel.Dock = DockStyle.Fill;
            btnPanel.FlowDirection = FlowDirection.RightToLeft;
            btnPanel.Margin = new Padding(0, 10, 0, 0);
            
            btnCancel = new SimpleButton();
            btnCancel.Text = "取消";
            btnCancel.Size = new Size(90, 32);
            btnCancel.Margin = new Padding(5, 0, 0, 0);
            btnCancel.DialogResult = DialogResult.Cancel;
            btnCancel.Click += BtnCancel_Click;
            
            btnOK = new SimpleButton();
            btnOK.Text = "确定";
            btnOK.Size = new Size(90, 32);
            btnOK.Margin = new Padding(0);
            btnOK.Appearance.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Bold);
            btnOK.Click += BtnOK_Click;
            
            btnPreview = new SimpleButton();
            btnPreview.Text = "预览";
            btnPreview.Size = new Size(90, 32);
            btnPreview.Margin = new Padding(0, 0, 5, 0);
            btnPreview.Click += BtnPreview_Click;
            
            btnPanel.Controls.Add(btnCancel);
            btnPanel.Controls.Add(btnOK);
            btnPanel.Controls.Add(btnPreview);
            
            panel.Controls.Add(btnPanel, 0, 3);
            
            this.Controls.Add(panel);
            this.AcceptButton = btnOK;
            this.CancelButton = btnCancel;
        }
        
        private void ListBoxSkins_SelectedValueChanged(object? sender, EventArgs e)
        {
            if (listBoxSkins.SelectedItem is SkinItem item)
            {
                SelectedSkin = item.Value;
                
                // 更新皮肤类型提示
                if (SkinManager.BonusSkins.Contains(item.Value))
                {
                    lblSkinType.Text = "✓ 外置皮肤（Bonus Skin）";
                    lblSkinType.Appearance.ForeColor = Color.FromArgb(0, 122, 204);
                }
                else if (SkinManager.DefaultBuiltInSkins.Contains(item.Value))
                {
                    lblSkinType.Text = "✓ 内置皮肤";
                    lblSkinType.Appearance.ForeColor = Color.FromArgb(128, 128, 128);
                }
                else
                {
                    lblSkinType.Text = "";
                }
            }
        }
        
        private void BtnPreview_Click(object? sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(SelectedSkin))
            {
                XtraMessageBox.Show("请先选择一个皮肤", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            
            try
            {
                // 尝试应用皮肤进行预览
                UserLookAndFeel.Default.SetSkinStyle(SelectedSkin);
                lblCurrentSkin.Text = $"当前预览: {SelectedSkin}";
                XtraMessageBox.Show($"已切换到皮肤: {SelectedSkin}\n\n点击\"确定\"保存此设置，或\"取消\"恢复原皮肤。", 
                    "预览", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show($"无法应用皮肤 '{SelectedSkin}':\n\n{ex.Message}\n\n可能原因：\n1. 该皮肤未安装\n2. 需要安装 DevExpress Bonus Skins 包", 
                    "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        private void BtnCancel_Click(object? sender, EventArgs e)
        {
            // 恢复原始皮肤
            try
            {
                UserLookAndFeel.Default.SetSkinStyle(originalSkin);
            }
            catch { }
        }
        
        private void LoadSkins()
        {
            var items = new List<SkinItem>();
            var availableSkins = SkinManager.GetAvailableSkins();
            
            foreach (var skin in availableSkins)
            {
                var skinType = SkinManager.BonusSkins.Contains(skin) ? "[外置] " : "[内置] ";
                items.Add(new SkinItem { Text = skinType + skin, Value = skin });
            }
            
            listBoxSkins.DataSource = items;
        }
        
        private void SelectCurrentSkin()
        {
            var currentSkin = SkinManager.GetCurrentSkin();
            SelectedSkin = currentSkin;
            
            // 选中当前皮肤
            for (int i = 0; i < listBoxSkins.ItemCount; i++)
            {
                var item = listBoxSkins.GetItem(i) as SkinItem;
                if (item != null && item.Value == currentSkin)
                {
                    listBoxSkins.SelectedIndex = i;
                    break;
                }
            }
        }
        
        private void BtnOK_Click(object? sender, EventArgs e)
        {
            if (listBoxSkins.SelectedItem is SkinItem selectedItem)
            {
                SelectedSkin = selectedItem.Value;
            }
            
            if (string.IsNullOrEmpty(SelectedSkin))
            {
                XtraMessageBox.Show("请选择一个皮肤", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            
            // 保存皮肤设置
            try
            {
                SkinManager.SaveSkin(SelectedSkin);
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                // 恢复原始皮肤
                try
                {
                    UserLookAndFeel.Default.SetSkinStyle(originalSkin);
                }
                catch { }
            }
        }
        
        private class SkinItem
        {
            public string Text { get; set; } = string.Empty;
            public string Value { get; set; } = string.Empty;
        }
    }
}
